/*
 * Copyright 2013 Solace Systems, Inc.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.solacesystems.tools.ant;

import org.xml.sax.Locator;

final class Location {
	public final String path;
	public final int line;
	public final int column;
	public final boolean isUnknown;
	
	public Location() {
		this(null, -1, -1);
	}
	
	public Location(String path, int line, int column) {
		this.isUnknown = path == null;
		this.path = isUnknown ? "<unknown>" : path;
		this.line = line;
		this.column = column;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (!this.isUnknown && obj instanceof Location) {
			Location other = (Location) obj;
			
			return (!other.isUnknown) &&
					(other.path.equals(this.path)) &&
					(other.line == this.line) &&
					(other.column == this.column);
		}
		
		return false;
	}
	
	@Override
	public int hashCode() {
		// Quick, hacky, hashCode implementation.
		return path.hashCode() ^ line ^ column;
	}
	
	@Override
	public String toString() {
		return path
			+ " <"
			+ line
			+ ","
			+ column
			+ ">";
	}

	public static Location fromLocator(Locator location) {
		return new Location(location.getSystemId(),
							location.getLineNumber(),
							location.getColumnNumber());
	}
}
